//
//  HomeScene.swift
//  aircraftBattle1
//
//  Created by 许洋 on 2024/5/16.
//

import SpriteKit
import AVFoundation

class HomeScene:BaseScene {
    private var bgNode:SKSpriteNode!
    private var playNode:SKSpriteNode!
    private var rankNode:SKSpriteNode!
    private var gangarNode:SKSpriteNode!
    private var battlegroundNode:SKSpriteNode!
    private var airIconNode:SKSpriteNode!
    private var shareNode:SKSpriteNode!

    private var avPlayerNode:AVAudioPlayer!
    private var selectIndex:Int = 0
    private var airTypes:Array<String> = []
    private var mapTypes:Array<String> = []
    private var mapSelectIndex:Int = 0

    
    override func didMove(to view: SKView) {
        bgNode = childNode(withName: "bg") as? SKSpriteNode
        bgNode.size = self.size
        playNode = childNode(withName: "Play") as? SKSpriteNode
        rankNode = childNode(withName: "Rank") as? SKSpriteNode

        gangarNode = childNode(withName: "Gangar") as? SKSpriteNode
        battlegroundNode = childNode(withName: "Battleground") as? SKSpriteNode
        airIconNode = gangarNode.childNode(withName: "AirIconNode") as? SKSpriteNode
        shareNode = childNode(withName: "Share") as? SKSpriteNode

        
        airTypes = ["air_m_y","air_m_b","air_m_g"]
        mapTypes = ["bg_0","bg_1","bg_2"]
        if !UserDefaults.standard.bool(forKey: "GANGAR") {
            UserDefaults.standard.set(0, forKey: "GANGAR")
        }
        if !UserDefaults.standard.bool(forKey: "BATTLEGROUND") {
            UserDefaults.standard.set(0, forKey: "BATTLEGROUND")
        }
        if !UserDefaults.standard.bool(forKey: "MAPTYPE") {
            UserDefaults.standard.set(mapSelectIndex, forKey: "MAPTYPE")
        }else{
            mapSelectIndex = UserDefaults.standard.integer(forKey: "MAPTYPE")
        }
        if !UserDefaults.standard.bool(forKey: "AIRTYPE") {
            UserDefaults.standard.set(mapSelectIndex, forKey: "AIRTYPE")
        }else{
            selectIndex = UserDefaults.standard.integer(forKey: "AIRTYPE")
        }

        let mapname = mapTypes[mapSelectIndex]
        bgNode.texture = SKTexture(imageNamed:mapname )
        let airname = airTypes[selectIndex]
        airIconNode.texture = SKTexture(imageNamed:airname )


        let bgMusic = SKAudioNode(fileNamed: "spaceBattle.mp3")
        bgMusic.autoplayLooped = true
        addChild(bgMusic)
     
        
    }
    func updateMapType(_ orientation: Int) {
        if orientation == 1{
            mapSelectIndex+=1
            if mapSelectIndex >= mapTypes.count{
                mapSelectIndex = 0
            }

        }else{
            mapSelectIndex-=1
            if mapSelectIndex < 0{
                mapSelectIndex = airTypes.count-1
            }

        }
        let name = mapTypes[mapSelectIndex]
        bgNode.texture = SKTexture(imageNamed:name )

        UserDefaults.standard.set(mapSelectIndex, forKey: "MAPTYPE")



    }

    func updateAirType(_ orientation: Int) {
        if orientation == 1{
            selectIndex+=1
            if selectIndex >= airTypes.count{
                selectIndex = 0
            }
            let name = airTypes[selectIndex]
            airIconNode.texture = SKTexture(imageNamed:name)

        }else{
            selectIndex-=1
            if selectIndex < 0{
                selectIndex = airTypes.count-1
            }
            let name = airTypes[selectIndex]
            airIconNode.texture = SKTexture(imageNamed:name )

        }
        UserDefaults.standard.set(selectIndex, forKey: "AIRTYPE")

        
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard  let touch = touches.first else {
            return
        }
        let touchLocation = touch.location(in: self)
        if  playNode.contains(touchLocation) {
            // 进入游戏
            // print("进入游戏")
            let reveal = SKTransition.doorsOpenVertical(withDuration: TimeInterval(0.5))
            let gameScene = BattleScene(fileNamed: "BattleScene")
            gameScene?.viewController = self.viewController
            gameScene?.size = self.size
            gameScene?.scaleMode = .aspectFill
            self.view?.presentScene(gameScene!, transition: reveal)
            
        }
        if  gangarNode.contains(touchLocation) {
            
            if touchLocation.x > 100{
                updateAirType(1)
            }else if touchLocation.x < -100{
                updateAirType(2)

            }
//            //飞机库
//            print("进入飞机库")
//            let reveal = SKTransition.doorsOpenHorizontal(withDuration: TimeInterval(0.5))
//            let gangarScene = GangarScene(fileNamed: "GangarScene")
//            gangarScene?.size = self.size
//            gangarScene?.scaleMode = .aspectFill
//            self.view?.presentScene(gangarScene!, transition: reveal)
            
        }
        if  battlegroundNode.contains(touchLocation) {
            if touchLocation.x > 100{
                updateMapType(1)
            }else if touchLocation.x < -100{
                updateMapType(2)
            }

        }
        if  rankNode.contains(touchLocation) {
            GameCenterManager.shared.authenticatePlayer(self.viewController!)
        }
        
        if  shareNode.contains(touchLocation) {
            shareText("")
        }



        
    }
    
    func shareText(_ text: String) {
        let textToShare = "https://apps.apple.com/us/app/lucky-win/id6502922624"
        let activityViewController = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)
            if let popover = activityViewController.popoverPresentationController {
            popover.sourceView = self.view
                popover.sourceRect = self.view!.bounds
        }
        self.viewController!.present(activityViewController, animated: true)
    }
}



